package main

import (
	"fmt"
	"sync"
	"time"
)

var (
	lock     = &sync.RWMutex{}
	resource = 0
)

func readResource() int {
	lock.RLock()
	defer lock.RUnlock()
	return resource
}

func updateResource(val int) {
	lock.Lock()
	defer lock.Unlock()
	resource = val
}

func reader() {
	for {
		r := readResource()
		fmt.Println("Reader:", r)
		time.Sleep(100 * time.Millisecond)
	}
}

func updater() {
	for i := 0; i < 5; i++ {
		updateResource(i)
		fmt.Println("Updated:", i)
		time.Sleep(500 * time.Millisecond)
	}
}

func main() {
	go reader()
	go updater()
	time.Sleep(3000 * time.Millisecond) // Keep the program running for a while
}
